﻿using RBAC.LuFang.Domain.Traceability;
using RBAC.LuFang.Repository.lnterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.LuFang.Domin.Services.TraceabilityService
{
    public class GoodsService: IGoodsService
    {
        private readonly IGoodsRepository _goodsRepository;
        private readonly IimgesRepository _imgRepository;
        private readonly IBreedInfoRepository _breedRepository;
        private readonly IhealthRepository _healthRepository;
        private readonly IEnterpriseRepository _enterRepository;

        public GoodsService(IGoodsRepository goodsRepository, IimgesRepository imgRepository, IBreedInfoRepository breedRepository, IhealthRepository healthRepository, IEnterpriseRepository enterRepository)
        {
            _goodsRepository = goodsRepository;
            _imgRepository = imgRepository;
            _breedRepository = breedRepository;
            _healthRepository = healthRepository;
            _enterRepository = enterRepository;
        }




        #region 商品

        /// <summary>
        /// 商品添加
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public async Task<int> AddGoods(Goods goods)
        {
            try
            {
                goods.addName = "张三";
                goods.addDate = DateTime.Now;
                var res = await _goodsRepository.Get(x => x.goodsName == goods.goodsName);
                if (res.Count > 0)
                {
                    return -2;//该商品已存在
                }
                await _goodsRepository.GetAddAsync(goods);
                var re = await _goodsRepository.GetByidAsync(goods.goodsId);
                if (re == null)
                {
                    return -1;//添加失败
                }
                return re.goodsId;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示 查询
        /// </summary>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">每页显示几条数据</param>
        /// <param name="goodsName">商品名称</param>
        /// <param name="goodsProductionBatch">生产批次</param>
        /// <param name="goodsperiod">保质期</param>
        /// <param name="goodsNumber">生产许可证号</param>
        /// <param name="goodsQiYe">生产企业</param>
        /// <returns></returns>
        public async Task<Goodspage> GetGoods(int pageindex, int pagesize, string? goodsName, string? goodsProductionBatch, string? goodsperiod, string? goodsNumber, string? goodsQiYe)
        {
            try
            {
                Goodspage goodspage=new Goodspage();
                var res = await _goodsRepository.GetAllAsync();
                if (!string.IsNullOrEmpty(goodsName))
                {
                    res = res.Where(x => x.goodsName.Contains(goodsName)).ToList();
                }
                if (!string.IsNullOrEmpty(goodsProductionBatch))
                {
                    res = res.Where(x => x.goodsProductionBatch.Contains(goodsProductionBatch)).ToList();
                }
                if (!string.IsNullOrEmpty(goodsperiod))
                {
                    res = res.Where(x => x.goodsperiod==goodsperiod).ToList();
                }
                if (!string.IsNullOrEmpty(goodsNumber))
                {
                    res = res.Where(x => x.goodsNumber.Contains(goodsNumber)).ToList();
                }
                if (!string.IsNullOrEmpty(goodsQiYe))
                {
                    res = res.Where(x => x.goodsQiYe.Contains(goodsQiYe)).ToList();
                }
                goodspage.sumcount = res.Count;
                goodspage.lastpage = (int)Math.Ceiling(goodspage.sumcount * 1.0 / pagesize);
                goodspage.list = res.ToList().OrderByDescending(x => x.addDate).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
                return goodspage;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 商品反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Goods> GetGoods(int id)
        {
            try
            {
                return await _goodsRepository.GetByidAsync(id);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> PiDelGoods(List<int> ids)
        {
            try
            {
                var res = (await _goodsRepository.GetAllAsync()).Where(x => ids.Contains(x.goodsId)).ToList();
                var list = 0;
                foreach (var item in res)
                {
                    list=await _goodsRepository.Del(item);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 商品修改
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public async Task<int> UpdGoods(Goods goods)
        {
            try
            {
                goods.updName = "李四";
                goods.updDate = DateTime.Now;
                return await _goodsRepository.GetUpd(goods);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="imges"></param>
        /// <returns></returns>
        public async Task<int> AddPic(imges imges)
        {
            try
            {
                var res = await _imgRepository.Get(x =>x.imgestype == imges.imgestype);
                if (res.Count() == 9)
                {
                    return -1;
                }
                return await _imgRepository.GetAddAsync(imges);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示产品检验报告图片
        /// </summary>
        /// <param name="imgtype"></param>
        /// <returns></returns>
        public async Task<List<imges>> GetChanPinImg(string imgtype,int goodsid)
        {
            try
            {
                var res = await _imgRepository.Get(x => x.imgestype == imgtype && x.goodsId==goodsid);
                return res.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除产品检验报告图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelChanPin(int id)
        {
            try
            {
                var res = await _imgRepository.GetByidAsync(id);
                return await _imgRepository.Del(res);
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion

        #region 养殖
        /// <summary>
        /// 养殖添加
        /// </summary>
        /// <param name="breed"></param>
        /// <returns></returns>
        public async Task<int> AddBreed(BreedInfo breed)
        {
            try
            {
                return await _breedRepository.GetUpd(breed);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据商品信息显示不同的养殖信息
        /// </summary>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        public async Task<List<BreedInfo>> GetBreed(int goodsid)
        {
            try
            {
                var res = await _breedRepository.Get(x => x.goodsId == goodsid);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 反填养殖信息
        /// </summary>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        public async Task<BreedInfo> FanBreed(int goodsid)
        {
            try
            {
                var res = (await _breedRepository.GetAllAsync()).Where(x => x.goodsId == goodsid).FirstOrDefault();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion

        #region 保健
        /// <summary>
        /// 添加保健信息
        /// </summary>
        /// <param name="health"></param>
        /// <returns></returns>
        public async Task<int> Addhealth(health health)
        {
            try
            {
                return await _healthRepository.GetAddAsync(health);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示保健列表
        /// </summary>
        /// <param name="breedid"></param>
        /// <returns></returns>
        public async Task<List<health>> Gethealth(int goodsId)
        {
            try
            {
                var res = await _healthRepository.GetAllAsync();
                var res1 = res.ToList().Where(x => x.goodsId == goodsId);
                return res1.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改保健
        /// </summary>
        /// <param name="health"></param>
        /// <returns></returns>
        public async Task<int> UpdHeath(health health)
        {
            try
            {
                return await _healthRepository.GetUpd(health);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除保健信息
        /// </summary>
        /// <param name="healthid"></param>
        /// <returns></returns>
        public async Task<int> Delhealth(int healthid)
        {
            try
            {
                var id = await _healthRepository.GetByidAsync(healthid);
                return await _healthRepository.Del(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示动检证照片
        /// </summary>
        /// <param name="imgtype"></param>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        public async Task<List<imges>> GetHeathImg(string imgtype, int goodsid)
        {
            try
            {
                var res = (await _imgRepository.GetAllAsync()).Where(x=>x.imgestype==imgtype && x.goodsId==goodsid);
                return res.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除动检证照片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelHeath(int id)
        {
            try
            {
                var res = await _imgRepository.GetByidAsync(id);
                return await _imgRepository.Del(res);
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion

        #region 企业
        /// <summary>
        /// 更新企业信息
        /// </summary>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        public async Task<int> AddEnterprise(Enterprise enterprise)
        {
            try
            {
                return await _enterRepository.GetUpd(enterprise);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 反填企业信息
        /// </summary>
        /// <param name="goodsId"></param>
        /// <returns></returns>
        public async Task<Enterprise> FanEnter(int goodsId)
        {
            try
            {
                var res = (await _enterRepository.GetAllAsync()).Where(x=>x.goodsId==goodsId).FirstOrDefault();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示企业信息
        /// </summary>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        public async Task<List<Enterprise>> GetEnter(int goodsid)
        {
            try
            {
                return await _enterRepository.Get(x=>x.goodsId==goodsid);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示养殖下的企业照片
        /// </summary>
        /// <param name="imgtype"></param>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        public async Task<List<imges>> GetEnterPic(string imgtype, int goodsid)
        {
            try
            {
                return (await _imgRepository.GetAllAsync()).Where(x=>x.imgestype==imgtype && x.goodsId==goodsid).ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除养殖下的企业照片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelEnterPic(int id)
        {
            try
            {
                var res = await _imgRepository.GetByidAsync(id);
                return await _imgRepository.Del(res);
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion


    }
}
